//
//  MessageDetailViewController.m
//  tristana
//
//  Created by 李康 on 2021/1/6.
//  Copyright © 2021 李康. All rights reserved.
//


#import "MessageDetailViewController.h"
#import<WebKit/WebKit.h>
#import <MBProgressHUD/MBProgressHUD.h>

@interface MessageDetailViewController ()<WKUIDelegate,WKNavigationDelegate>

@property (strong, nonatomic) WKWebView *mainWebView;


@end

@implementation MessageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    self.mainWebView  = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    [self.view addSubview:self.mainWebView];
    
    self.mainWebView.UIDelegate = self;
    self.mainWebView.navigationDelegate = self;
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *leftImg = [UIImage imageNamed:@"loginBack"];
    leftBtn.frame = CGRectMake(0, 0, leftImg.size.width, leftImg.size.height);
    [leftBtn setImage:leftImg forState:UIControlStateNormal];
    
    [leftBtn addTarget:self action:@selector(leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    NSString* path = [[NSBundle mainBundle] pathForResource:self.contentUrl ofType:@"html"];
    NSData* htmldata = [NSData dataWithContentsOfFile:path];
    NSString* htmlcontent = [[NSString alloc] initWithData:htmldata encoding:NSUTF8StringEncoding];
    NSString *imagePath  = [[NSBundle mainBundle] bundlePath];
    [self.mainWebView loadHTMLString:htmlcontent baseURL:[NSURL fileURLWithPath:imagePath]];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
}

-(void)leftBarAction:(id)sender{
     [self.navigationController popViewControllerAnimated:YES];
}

//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"开始加载网页");
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加载完成");
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


@end
